<?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-04-15 08:58:26
 * @version 1.0
 */
namespace Admin\Controller;

class MentorsController extends AdminController {
	public function index() {
		$nickname = I ( "nickname" );
		$city = I ( "city" );
		$state = I ( "state" );
		$code = I ( "code" );
		if ($nickname) {
			$map ['nickname'] = array (
					'like',
					"%" . $nickname . "%" 
			);
			$this->assign ( "nickname", $nickname );
		}
		if ($city) {
			$map ['city'] = $city;
			$this->assign ( "city", $city );
		}
		if ($state) {
			$map ['state'] = $state;
			$this->assign ( "state", $state );
		}
		if ($code) {
			$map ['zip'] = $code;
			$this->assign ( "code", $code );
		}
		
		$map ['del'] = 0;
		$obj = M ( 'Mentors' );
		
		$count = $obj->where ( $map )->count ();
		
		import ( "Common.ORG.Page" );
		
		$page = new \Page ( $count, 20 );
		
		$show = $page->show ();
		
		$list = $obj->where ( $map )->order ( 'id desc' )->limit ( $page->firstRow . ',' . $page->listRows )->select ();
		
		foreach ( $list as $key => $value ) {
			
			$value ['trainItem'] = trainStrtoArr ( $value ['trainItem'] );
			
			$where ['id'] = array (
					'in',
					$value ['trainItem'] 
			);
			
			$list [$key] ['trainItem'] = M ( 'TrainCate' )->field ( 'name' )->where ( $where )->select ();
		}
		
		$this->assign ( 'list', $list );
		
		$this->assign ( 'page', $show );
		
		$stateList = M ( 'States' )->select ();
		$this->assign ( 'stateList', $stateList );
		
		$this->display ();
	}

	public function add(){
		$map['show']=1;
        $train = M('TrainCate')->where($map)->order('sort asc')->select();
        $this->assign('train',$train);

        $state = M('States')->select();
        $this->assign('state',$state);

        $nowYear = date('Y')-10;
        $this->assign('nowYear',$nowYear);
		$this->display();
	}

	public function save(){
		$obj = M('Member');
        if ($data = $obj->create()) {
        	$map['username'] = $data['username'];
        	$list = $obj->where($map)->find();
        	if ($list) {
        		echo $this->echo_json_str('The email address is already exist.',$message,$url); 
        		die;
        	}

        	unset($map);
	        $map['nickname'] = I('post.nickname');
	        $list = M('Mentors')->where($map)->find();
	        if ($list) {
	            echo $this->echo_json_str('Validation of the nickname be repeated by other profile nickname',$message,$url); 
	            die;
	        }

    		$data['password'] = md5($data['password']);
    		$data['userType'] = 2;
    		$data['status'] = 1;
    		$data['regDate'] = time();
    		$data['regIP'] = get_client_ip();
    		$data['loginDate'] = time();
    		$data['loginIP'] = get_client_ip();
    		if ($userID = $obj->add($data)) {

    			unset($data);
    			$data['trainItem'] = I('post.trainItem');
		        if ($data['trainItem']=='') {
		            $state = 'Please choose the training program';
		            echo $this->echo_json_str($state,$message,$url); die;
		        }else{
		            $data['trainItem'] = '-'.implode('-', $data['trainItem']).'-';
		        }

		        if (!empty($_FILES['file']['tmp_name'])) {
		            $file = $this->_upload();
		            if($file['state']=="success"){
		                $data['resume'] = $file['resume'];
		                $data['document'] = $file['name'];
		            }else{
		            	$map['userid']=$userID;
	        			$obj->where($map)->delete();
		                echo $this->echo_json_str($file['msg']); die;
		            }			            
		        }

    			$data['nickname'] = I('post.nickname');
		        $data['firstName'] = I('post.firstName');
		        $data['lastName'] = I('post.lastName');
		    	$data['gender'] = I('post.gender');
		    	$data['birthday'] = I('post.birthday');
		    	$data['address'] = I('post.address');
		        $data['city'] = I('post.city');
		        $data['state'] = I('post.state');
		        $data['zip'] = I('post.zip');
		        $data['exp'] = I('post.exp');
		    	$data['phone'] = I('post.phone');
		        $data['skill'] = I('post.skill');
		        $data['working'] = I('post.working');        
		     	$data['description'] = I('post.description');
		     	$data['userid'] = $userID;
		    	$data['updateTime'] = time();
	        	$data['createTime'] = time();
	        	$list = M('Mentors')->add($data);

	        	if ($list) {
	        		$state = 'SUCCESS';
	                $message = L('_action_success');
	                $url = U('Mentors/index');
	        	}else{
	        		$map['userid']=$userID;
	        		$obj->where($map)->delete();
	        		$state = L('_action_fail');
	        	}             
            } else {
                $state = L('_action_fail');
            }
        	          
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url); 
	}
	
	// 查看详情
	public function detail() {
		$id = ( int ) $_GET ['id'];
		
		if (! isset ( $id )) {
			
			$this->error ( L ( "_parm_error" ) );
		}
		
		$obj = M ( 'Mentors' );
		
		$list = $obj->where ( "id=$id" )->find ();
		
		if (! $list) {
			
			$this->error ( L ( "_public_noinfo" ) );
		} else {
			
			$list ['email'] = M ( 'Member' )->where ( 'userid=' . $list ['userid'] )->getField ( 'username' );
			
			$list ['trainItem'] = trainStrtoArr ( $list ['trainItem'] );
			
			$where ['id'] = array (
					'in',
					$list ['trainItem'] 
			);
			
			$list ['trainItem'] = M ( 'TrainCate' )->field ( 'name' )->where ( $where )->select ();
			
			$this->assign ( 'list', $list );
			
			$this->display ();
		}
	}
	
	// 编辑更新
	public function edit() {
		$id = ( int ) I ( "post.id" );
		
		$status = I ( "post.status" );
		
		$map ['id'] = $id;
		
		M ( "Mentors" )->where ( $map )->setField ( "status", $status );
		
		$this->success ( L ( '_action_success' ) );
	}
	
	// 删除
	public function del() {
		$id = I ( 'post.selectedids' );
		
		if ($id == '') {
			
			$state = L ( "_no_selected" );
		} else {
			
			$obj = M ( 'Mentors' );
			
			foreach ( $id as $v ) {
				
				$map ['id'] = $v;
				
				$obj->where ( $map )->delete ();
			}
			
			$state = 'SUCCESS';
			
			$message = L ( '_action_success' );
			
			$url = U ( 'Mentors/index' );
		}
		
		echo $this->echo_json_str ( $state, $message, $url );
	}

	#文件上传
    private function _upload(){
        $path = '.'.C('UPLOAD_PATH').'resume/';
        //import("ORG.Net.UploadFile");
        $upload = new \Think\Upload();// 实例化上传类
        //$upload=new UploadFile();
        $upload->maxSize = 1000000;  //是指上传文件的大小，默认为-1,不限制上传文件大小bytes
        $upload->rootPath = $path;   //上传保存到什么地方？路径建议大家已主文件平级目录或者平级目录的子目录来保存
        $upload->saveRule=uniqid;    //上传文件的文件名保存规则  time uniqid  com_create_guid  uniqid
        $upload->uploadReplace=true;     //如果存在同名文件是否进行覆盖
        $upload->exts= array('doc','pdf');     //准许上传的文件后缀
        //$upload->allowTypes= array('application/vnd.ms-excel'); //检测mime类型
        $info = $upload->upload();
        if($info){
            foreach($info as $file){
                if (empty($dir)) {
                    $url = $file['savepath'].$file['savename'];
                }else{
                    $url = $dir.'/'.$file['savepath'].$file['savename'];
                }
                $name = $file['name'];
            }  

            return array(
                'state'=>"success",
                'name' => $name,
                'resume' => C('UPLOAD_PATH').'resume/'.$url,
                );
            
        }else{
            //是专门来获取上传的错误信息的 
            return array(
                'state'=>"error",
                'msg' => $upload->getError()
                );           
        }
    }
}